package com.example.controller;

import com.example.pojo.Defect;
import com.example.pojo.PageResult;
import com.example.pojo.Result;
import com.example.service.DefectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 缺陷种类管理控制器
 */
@Slf4j
@RequestMapping("/defects")
@RestController
public class DefectController {

    @Autowired
    private DefectService defectService;

    /**
     * 分页查询缺陷种类 - GET http://localhost:8881/defects/nomachine?page=1&pageSize=10
     */
    @GetMapping("/nomachine")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询缺陷种类，当前页码为：{}，每页记录数：{}", page, pageSize);
        PageResult pageResult = defectService.page(page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 分页查询设备源缺陷种类 - GET /defects/ismachine?page=1&pageSize=10
     */
    @GetMapping("/ismachine")
    public Result pagem(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询缺陷种类，当前页码为：{}，每页记录数：{}", page, pageSize);
        PageResult pageResult = defectService.pagem(page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 新增缺陷种类 - POST /defects
     */
    @PostMapping
    public Result save(@RequestBody com.example.pojo.Defect defect) {
        log.info("新增缺陷种类，缺陷种类信息为：{}", defect);
        defectService.save(defect);
        return Result.success();
    }

    /**
     * 删除缺陷种类 - DELETE /defects/1
     */
    @DeleteMapping
    public Result delete(Integer defectID){
        log.info("删除缺陷种类，id为：{}", defectID);
        defectService.deleteById(defectID);
        return Result.success();
    }

    /**
     * 修改缺陷种类 - PUT /defects
     */
    @PutMapping
    public Result update(@RequestBody Defect defect){
        log.info("修改缺陷种类，修改后的数据为：{}", defect);
        defectService.update(defect);
        return Result.success();
    }

    /**
     * 获取所有标注后缺陷种类名称 - GET http://localhost:8881/defects/allnames
     */
    @GetMapping("/allnames")
    public Result getAllNames() {
        log.info("获取所有缺陷种类名称");
        return Result.success(defectService.getAllNames());
    }

    /**
     * 获取所有设备源缺陷种类名称 - GET http://localhost:8881/defects/allmachinenames
     */
    @GetMapping("/allmachinenames")
    public Result getAllMachineNames() {
        log.info("获取所有设备源缺陷种类名称");
        return Result.success(defectService.getAllMachineNames());
    }

}
